package com.longtxt.powerband.ota

import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.content.Context
import com.jieli.jl_bt_ota.constant.BluetoothConstant
import com.jieli.jl_bt_ota.impl.BluetoothOTAManager
import com.jieli.jl_bt_ota.model.BluetoothOTAConfigure
import com.jieli.jl_rcsp.constant.StateCode
import com.jieli.jl_rcsp.impl.RcspAuth
import com.longtxt.powerband.core.BlePeripheral


class OtaManager(context: Context) : BluetoothOTAManager(context) {

    private var blePeripheral: BlePeripheral? = null

    var isOtaProcess = false

    fun clearBlePeripheral() {
        blePeripheral = null
    }

    fun setUpBlePeripheral(peripheral: BlePeripheral) {
        blePeripheral = peripheral
    }

    fun initOTASdk() {
        val bluetoothOption = BluetoothOTAConfigure()
        //选择通讯方式
        bluetoothOption.priority = BluetoothOTAConfigure.PREFER_BLE

        //是否需要自定义回连方式(默认不需要，如需要自定义回连方式，需要客户自行实现)
        bluetoothOption.isUseReconnect = true
        //是否启用设备认证流程(与固件工程师确认)
        bluetoothOption.isUseAuthDevice = false
        //设置BLE的MTU
        bluetoothOption.mtu = BluetoothConstant.BLE_MTU_MIN
        //是否需要改变BLE的MTU
        bluetoothOption.isNeedChangeMtu = false
        //是否启用杰理服务器(暂时不支持)
        bluetoothOption.isUseJLServer = false
        //配置OTA参数
        configure(bluetoothOption)
        RcspAuth.setAuthTimeout(5000)
        onBtDeviceConnection(connectedDevice, StateCode.CONNECTION_OK)
    }


    fun changeToPreparedState() {
        onBtDeviceConnection(connectedDevice, StateCode.CONNECTION_OK)
    }

    override fun getConnectedDevice(): BluetoothDevice? {
        return blePeripheral?.mDevice
    }

    override fun getConnectedBluetoothGatt(): BluetoothGatt? {
        return blePeripheral?.mGatt
    }

    override fun connectBluetoothDevice(bluetoothDevice: BluetoothDevice?) {
        //仅仅作为回连设备，回连设备现在仅支持BLE
        val result: Boolean = blePeripheral?.connect() == true
        if (!result) {
            onBtDeviceConnection(bluetoothDevice, StateCode.CONNECTION_FAILED)
        }
    }


    fun notifyConnectionState(device: BluetoothDevice, state: Int) {
        if (state != 2) {
            onBtDeviceConnection(device, convertWatchConnectStatus(state))
        }
    }


    override fun disconnectBluetoothDevice(bluetoothDevice: BluetoothDevice?) {
        blePeripheral?.disconnect()
    }

    override fun sendDataToDevice(bluetoothDevice: BluetoothDevice?, bytes: ByteArray?): Boolean {

        if (blePeripheral == null || bytes == null) {
            return false
        }
        return blePeripheral!!.writeJLRequest(bytes)
    }

    private fun convertWatchConnectStatus(status: Int): Int {
        val newStatus: Int = when (status) {
            com.jieli.bluetooth_connect.constant.BluetoothConstant.CONNECT_STATE_CONNECTING -> StateCode.CONNECTION_CONNECTING
            com.jieli.bluetooth_connect.constant.BluetoothConstant.CONNECT_STATE_CONNECTED -> StateCode.CONNECTION_OK
            else -> StateCode.CONNECTION_DISCONNECT
        }
        return newStatus
    }

}
